///////////////////////////////////////////////////////////////////////
// File:        entry.cpp
// Description: defines the application entry point
// Author:      Philip Kommnick
// Created:     Mon August 31 16:04:48 CEST 2009
//
// This program is open source. For license terms, see the LICENSE file.
//
///////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EventManager.h"

HINSTANCE tpInstance;

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR cmdLine, int nShowCmd)
{
	tpInstance = hInst;
    HANDLE singleInst = CreateMutex(NULL, FALSE, TP_MUTEX);
    if (GetLastError()==ERROR_ALREADY_EXISTS) {
        HWND hWnd = FindWindow(TP_WINCLASS, NULL);
        if (!PostMessage(hWnd, WM_TPCLOSE, 0, 0))
			//TaskDialog(hWnd, tpInstance, MAKEINTRESOURCE(IDS_APPNAME), MAKEINTRESOURCE(IDS_APPQUITINST), MAKEINTRESOURCE(IDS_APPQUITCONT), TDCBF_CLOSE_BUTTON, MAKEINTRESOURCE(IDI_APP), NULL);
            MessageBox(NULL, L"Could not send quit command!", L"Fatal Error", MB_ICONERROR | MB_OK);
        return 0;
    }
    //INITCOMMONCONTROLSEX *cc6 = new INITCOMMONCONTROLSEX();
    //cc6->dwSize=sizeof(cc6);
    //cc6->dwICC= ICC_WIN95_CLASSES | ICC_PROGRESS_CLASS | ICC_STANDARD_CLASSES;
    //InitCommonControlsEx(cc6);
    //delete cc6;

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

	EventManager *em = new EventManager();
    MSG Msg;
    while (GetMessage(&Msg, NULL, 0, 0) > 0) {
		if (em->PreTranslateMessage(Msg))
			continue;
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
    }
    delete em;
    CoUninitialize();
    CloseHandle(singleInst);
    return 0;
}